package active.mediator;

import constructor.decorator.Coffee;

import java.util.HashMap;

public class ConcreteMeditor implements Mediator {

    private HashMap<String, Colleague> colleagueHashMap;
    private HashMap<String, String> interMap;

    public ConcreteMeditor() {
        this.colleagueHashMap = new HashMap<>();
        this.interMap = new HashMap<>();
    }

    @Override
    public void register(String colleagueName, Colleague colleague) {
        colleagueHashMap.put(colleagueName, colleague);

        if (colleague instanceof Alarm) {
            interMap.put("Alarm", colleagueName);
        } else if (colleague instanceof TV) {
            interMap.put("TV", colleagueName);
        } else if (colleague instanceof CoffeeMachine) {
            interMap.put("CoffeeMachine", colleagueName);
        } else if (colleague instanceof Curtains) {
            interMap.put("Curtains", colleagueName);
        }
    }

    /**
     * 完成调度任务
     *
     * @param stateChange
     * @param colleagueName
     */
    @Override
    public void getMessage(int stateChange, String colleagueName) {
        Colleague colleague = colleagueHashMap.get(colleagueName);

        if (colleague instanceof Alarm) {
            if (stateChange == 0) {
                String coffeeMachine = interMap.get("CoffeeMachine");
                CoffeeMachine coffeeMachine1 = (CoffeeMachine) colleagueHashMap.get(coffeeMachine);
                coffeeMachine1.startCoffee();
            } else if (stateChange == 1) {
                String tvName = interMap.get("TV");
                TV tv = (TV) colleagueHashMap.get(tvName);
                tv.stopTV();
            }
        } else if (colleague instanceof CoffeeMachine) {
            String curtainsName = interMap.get("Curtains");
            Curtains curtains = (Curtains) colleagueHashMap.get(curtainsName);
            curtains.upCurtains();
        }
    }

    @Override
    public void sendMessage() {

    }
}
